-- SkyFighter
-- Create by panyl
-- 空战对象

SkyFighter = {
    -- 军队
    army = {},

    -- 机械人
    robots = {},

    -- 数据
    dbase = {},

    -- 实体类型
    type = OBJECT_TYPE_SKYFIGHTER,
};
SkyFighter.__index = SkyFighter;

-- 构造函数
function SkyFighter.new(info, pos)
    local self = {};
    setmetatable(self, SkyFighter);

    self.army = info["pets"] or {};

    self.dbase = Dbase.new(info);

    -- 血量
    if type(info["hp"]) == "number" then
        self.dbase:set("hp", info["hp"]);
    elseif type(info["left_hp"]) == "number" then
        self.dbase:set("hp", info["left_hp"]);
    else
        self.dbase:set("hp", info["fight_ability"] or 0);
    end

    -- 昵称
    self.dbase:set("name", info["name"]);

    -- 战斗对象的坐标
    -- 攻击方为 SKY_CITY_ATTACKER
    -- 防守方为 SKY_CITY_DEFENSER
    self.pos = pos;
    self.dbase:set("pos", pos);

    return self;
end

-- 取得该对象的描述字符串
function SkyFighter:get_ob_id()
    return string.format("SkyFighter:%s", self:getName());
end

-- 名字
function SkyFighter:getName()
    return getNickName(self.dbase:query("name", ""));
end

-- 获取所有秘宝技能
function SkyFighter:getAllSkills()
    local skills = self.dbase:query("ship_skills");
    if skills then
        return skills;
    end

    local id = self.dbase:query("id");
    return SkyExploreM.query(id, "ship_skills") or {};
end

-- 获取所有的被动技能
function SkyFighter:getAllPassive()
    local skills = self:getAllSkills();
    local ret = {};
    local skillId;

    for _, skillId in pairs(skills) do
        if SkyShipSkillM.query(skillId, "type") == SHIP_SKILL_PASSIVE then
            table.insert(ret, skillId);
        end
    end

    return ret;
end

function SkyFighter:getPos()
    return 0;
end

-- 获取军队
function SkyFighter:getArmy()
    if self:isPvp() then
        -- 玩家，返回所有出战宠物
        return self.dbase:query("pets", {});
    end

    return self.army or {};
end

-- 获取军队对应时装
function SkyFighter:getArmyCostumes()
    return self.dbase:query("pet_costumes") or {};
end

-- 获取出战的机械人
function SkyFighter:getRobots()
    -- 返回所有出战的机器人
    local list = {};
    for robotId, num in pairs(self.dbase:query("robots", {})) do
        for i = 1, num do
            table.insert(list, robotId);
        end
    end
    return list;
end

-- 是否pvp
function SkyFighter:isPvp()
    if self.dbase:query("is_pvp") == 1 then
        return true;
    end

    -- 目前只有对战玩家才是pvp
    local id = self.dbase:query("id");
    if not id then
        return false;
    end

    return SkyExploreM.query(id, "rule") == "player";
end

-- 舰队物理攻击触发
function SkyFighter:attackTrigger()
    -- TODO: 需要等秘宝功能
    return 0;
end

-- 舰队受创触发反击
function SkyFighter:hitTrigger()
    -- TODO: 秘宝的功能
    return 0;
end

-- 扣除属性
function SkyFighter:costAttrib(attrib, value)
    local cur = self.dbase:query(attrib, 0);

    self.dbase:set(attrib, cur - value);
end

-- 受创
function SkyFighter:receiveDamage(damage, addAction)
    -- 修正下，不能小于0
    local curHp = self:queryAttrib("hp");

    self:costAttrib("hp", iif(damage > curHp, curHp, damage));

    if addAction then
        local battle    = self:getBattle();
        local sequence  = battle.sequenceList:getSequenceByFighter(self);

        sequence:receiveDamage(target, 0, damage);

    end
    -- TODO: 抛出事件？
end

-- 复活
function SkyFighter:revive(value)
    self.dbase:set("hp", value);
end

-- 增加属性
function SkyFighter:addAttrib(attrib, value)
    local cur = self.dbase:query(attrib, 0);

    self.dbase:set(attrib, cur + value);
end

-- 查询属性
function SkyFighter:queryAttrib(attrib)
    -- 查询属性
    return AttribM.queryAttrib(self, attrib);
end

-- 获取是攻击方还是防守方
function SkyFighter:getStyle()
    if self.pos == SKY_CITY_ATTACKER then
        return "attacker";
    else
        return "defenser";
    end
end

-- 获取战场
function SkyFighter:getBattle()
    return self.dbase:query("owner");
end
